Option Strict Off
Option Explicit On
Module ConcatenateJpg
	
	'This module recieves a directory and a frame rate and joins all the jpeg files in it to form a motion jpeg file.
	
	Public Function ConcatJPG(ByRef strPath As String, ByRef FrameRateB As Integer) As Boolean
		ConcatJPG = False 'Set function to initially return an error
		
		Dim File As String
		Dim NameList() As String 'A string array holding the names of all the frames
		Dim NumFramesToAdd As Integer 'The total number of frames in the directory
		Dim i As Integer 'simple loop variable

		
		'Check if directory exists
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(strPath, FileAttribute.Directory) = "" Then
			Call MsgBox("I could not find directory - " & strPath, MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Function 'Exit the Subroutine
		End If
		
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		File = Dir(strPath & "*.jpg") 'Checks a file exists which matches the wildcard and returns the filename eg "D:\Visual Basic\Temp\*.jpg"
		Do While Len(File) 'loop whilst the retrieved filename has a string length greater than zero
			ReDim Preserve NameList(NumFramesToAdd + 1) 'resize the array to take the new filename
			NameList(NumFramesToAdd) = File 'Copy the filename into the array
			NumFramesToAdd = NumFramesToAdd + 1 'Increment the frame count
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			File = Dir() 'Get the next filename
		Loop 
		
		'Check if there are any frames in the directory
		If NumFramesToAdd = 0 Then 'If there are no frames found , alert the user
			Call MsgBox("I could not find any .jpg frames in the specified directory.", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Function 'Exit the Subroutine
		End If
		
		'Generate the output filename
		If CDbl(GenerateFilename(strPath)) = 0 Then 'The function has failed to generate a valid filename
			Call MsgBox("I could not generate a filename in the specified directory.", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Function 'Exit the Subroutine
		End If
		
		
		
		For i = 0 To NumFramesToAdd - 1 'Loop through the array from lowest to highest
			File = strPath & NameList(i)
			frmUtilities2.imgFrameDisplay.Image = System.Drawing.Image.FromFile(File) 'Load the picture to the utilities picture window
			frmUtilities2.Refresh() 'Refresh the form to show the image
			frmUtilities2.imgJPEGSize.Image = System.Drawing.Image.FromFile(File) 'Load the image to the invisible but resizable image box to get image sizes
			frmUtilities2.List1.Items.Add(File & "    " & frmUtilities2.imgJPEGSize.Width & " x " & frmUtilities2.imgJPEGSize.Height) 'Add the image details to the list window
			'UPGRADE_ISSUE: ListBox property List1.NewIndex was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="F649E068-7137-45E5-AC20-4D80A3CC70AC"'
            frmUtilities2.List1.SelectedIndex = frmUtilities2.List1.Items.Count - 1 'Scroll listbox down to show last added item
			
			FunctionOK = AppendMJpeg(strPath & ValidFilename, File, FrameRateB, (frmUtilities2.imgJPEGSize.Width), (frmUtilities2.imgJPEGSize.Height)) 'Append the JPEG image to the AVI file. Creates file if necessary.
			
		Next i
		
		frmUtilities2.List1.Items.Add(" ")
		frmUtilities2.List1.Items.Add(ValidFilename & " generated sucessfully. " & NumFramesToAdd & " Frames at " & FrameRateB & " Frames per second")
		'UPGRADE_ISSUE: ListBox property List1.NewIndex was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="F649E068-7137-45E5-AC20-4D80A3CC70AC"'
        frmUtilities2.List1.SelectedIndex = frmUtilities2.List1.Items.Count - 1 'Scroll listbox down to show last added item
		
		ConcatJPG = True 'Set function to return an "true" to tell the main program it was successful
	End Function
End Module